<?php

class Defined_Upload{

	function __construct(){

	}

	function doUpload($name, $folder='logo'){
		if($_FILES[$name]['name']!=''){
			if (($_FILES[$name]["size"]/1024 < 1024))
			{
				if((($_FILES[$name]["type"] == "image/gif") || ($_FILES[$name]["type"] == "image/jpeg") || ($_FILES[$name]["type"] == "image/pjpeg") || ($_FILES[$name]["type"] == "image/png"))){
					if ($_FILES[$name]["error"] > 0){
						return 3; //file có lỗi
					}else{
						$name_file = substr(strtolower($_FILES[$name]["name"]),0,strlen($_FILES[$name]["name"])-4);
						$ext = substr(strtolower($_FILES[$name]["name"]),-3);
						$convert_name = md5($name_file.time()).'.'.$ext;
						$path=$_FILES[$name]['tmp_name'];

						//build thumbnail
						$thumb = $this->make_thumb($convert_name,$path,'default','true');
						//move_uploaded_file($_FILES[$name]["tmp_name"], $path);
						$arrDate = getdate();
							
						return $arrDate['year'].'/'.$arrDate['mon'].'/'.$convert_name;
						//return $convert_name;
					}
				} else{
					return 1; //lỗi không đúng định dạng
				}
			}else{
				# invalid file
				return 2;  // dung lượng  lớn hơn cho phép
			}
		}
		return '';
	}

	function make_thumb($img_name,$path,$type='default',$datefolder='true',$fpath='')
	{
		$arrayTmp = Zend_Registry::get('config_site');
		$arrBlogConfig = $arrayTmp[$type];
		$ext=$this->getExtension($img_name);
		//creates the new image using the appropriate function from gd library

		foreach($arrBlogConfig as $key => $tmpBlog)
		{
			if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
			$src_img=imagecreatefromjpeg($path);

			if(!strcmp("png",$ext))
			$src_img=imagecreatefrompng($path);

			if(!strcmp("gif",$ext))
			$src_img=imagecreatefromgif($path);


			//gets the dimmensions of the image
			$old_x=imageSX($src_img);
			$old_y=imageSY($src_img);
			$new_w = $tmpBlog['width'];
			$new_h=$tmpBlog['height'];
			// next we will calculate the new dimmensions for the thumbnail image
			// the next steps will be taken:
			// 1. calculate the ratio by dividing the old dimmensions with the new ones
			// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
			// and the height will be calculated so the image ratio will not change
			// 3. otherwise we will use the height ratio for the image
			// as a result, only one of the dimmensions will be from the fixed ones

			/*	$ratio1=$old_x/$new_w;
			 $ratio2=$old_y/$new_h;
			 if($ratio1>$ratio2) {

				$thumb_w=$new_w;
				$thumb_h=$old_y/$ratio1;
				}
				else {
				$thumb_h=$new_h;
				$thumb_w=$old_x/$ratio2;
				}*/
			$ratio1=$old_x/$new_w;
			$ratio2=$old_y/$new_h;
			if($ratio1>$ratio2 && $ratio1 > 1) {
				if($new_w < $old_x) {
					$thumb_w=$new_w;
					$thumb_h=$old_y/$ratio1;
				} else {
					$thumb_w = $old_x;
					$thumb_h=$old_y/$ratio1;
				}
			} else {
				if($new_h < $old_y) {
					$thumb_w=$new_w;
					$thumb_h=$old_y/$ratio1;
				} else {
					$thumb_w = $old_x;
					$thumb_h = $old_y;
				}
			}



			// we create a new image with the new dimmensions
			$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

			// resize the big image to the new created one
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
			if($datefolder=='true')
			{
				$arrDate = getdate();
				$dirPathYear = UPLOAD.$fpath.$type.'/'.$key.'/'.$arrDate['year'];
				$dirPathMonth = $dirPathYear.'/'.$arrDate['mon'].'/';

				if(!is_dir($dirPathMonth)){
					mkdir($dirPathMonth,0777,true);
				}
				$filename=$dirPathMonth.$img_name;
			}
			else
			{
				$filename=UPLOAD.$fpath.$type.'/'.$key.'/'.$img_name;
			}


			// output the created image to the file. Now we will have the thumbnail into the file named by $filename
			if(!strcmp("png",$ext))
			imagepng($dst_img,$filename);
			else if(!strcmp("jpg",$ext)||!strcmp("jpeg",$ext))
			imagejpeg($dst_img,$filename);
			else if(!strcmp("gif",$ext))
			imagegif($dst_img,$filename);

			//destroys source and destination images.
			imagedestroy($dst_img);
			imagedestroy($src_img);
		}
	}

	function getExtension($str)
	{
		$i = strrpos($str,".");
		if (!$i) { return ""; }
		$l = strlen($str) - $i;
		$ext = substr($str,$i+1,$l);
		return $ext;
	}
}
#echo "Upload: " . $_FILES["image"]["name"] . "<br />";
#echo "Type: " . $_FILES["image"]["type"] . "<br />";
#echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
#echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br />";

?>